#region Usings

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Castle.ActiveRecord;
using Newtonsoft.Json;
using NUnit.Framework;
using Karma.Framework.Core.Repository;
using Karma.Framework.Core.Repository.Configuration;
using Karma.Framework.Core.Serialization.Json;
using Karma.Framework.Core.Serialization.Json.Converters;
using Karma.Framework.Core.Test.Repository.Domain;

#endregion

namespace Karma.Framework.Core.Test.Serialization.Json
{
    [TestFixture]
    public class NHibernateJsonSerializerFixture
    {
        #region Setup/Teardown

        [SetUp]
        public void SetUp()
        {
            ActiveRecordConfiguration.Initialize();
            // inicializa el esquema -------------------
            ActiveRecordStarter.DropSchema();
            ActiveRecordStarter.CreateSchema();
        }

        [TearDown]
        public void TearDown()
        {
            ActiveRecordMediator<Estado>.DeleteAll();
            ActiveRecordMediator<Pais>.DeleteAll();
        }

        #endregion

        [Test]
        public void SerializeQuerySpecificationTest()
        {
            var specification = new QuerySpecification
                                    {
                                        Query = "from Pais p where (p.Nombre like ?)",
                                        Criteria = "Es",
                                        Start = 0,
                                        PageSize = 5,
                                        Sorting = new[]
                                                      {
                                                          new FieldSortSpecification
                                                              {
                                                                  Field = "Nombre",
                                                                  Sort = SortEnum.Asc
                                                              }
                                                      }
                                    };

            var serializer = new NHibernateProxyJsonSerializer();
            var writer = new StringWriter();
            serializer.Serialize(new JsonTextWriter(writer), specification);
            var json = writer.ToString();

            Assert.IsNotNull(json);
            Assert.IsNotEmpty(json);

            var reader = new StringReader(json);
            var spectmp =
                (QuerySpecification)
                serializer.Deserialize(new JsonTextReader(reader), typeof (QuerySpecification));

            Assert.IsNotNull(spectmp);
            Assert.IsNotNull(spectmp.Query);
            Assert.IsTrue(spectmp.Query.Equals(specification.Query));
            Assert.IsNotNull(spectmp.Criteria);
            Assert.IsTrue(spectmp.Criteria.Equals(specification.Criteria));
            Assert.IsTrue(spectmp.Start == specification.Start);
            Assert.IsTrue(spectmp.PageSize == specification.PageSize);
            Assert.IsNotNull(spectmp.Sorting);
            Assert.IsNotEmpty(spectmp.Sorting.ToArray());
            Assert.IsTrue(spectmp.Sorting.Length == 1);
        }

        [Test]
        public void SimpleSerializationTest()
        {
            var pais = new Pais {Nombre = "Mexico", Activo = true, FechaAdd = DateTime.Now};
            pais.Estados = new List<Estado>
                               {
                                   new Estado {Nombre = "Aguascalientes", Pais = pais},
                                   new Estado {Nombre = "Monterrey", Pais = pais},
                                   new Estado {Nombre = "Tabasco", Pais = pais},
                                   new Estado {Nombre = "Guerrero", Pais = pais},
                                   new Estado {Nombre = "Chiapas", Pais = pais},
                                   new Estado {Nombre = "Chihuahua", Pais = pais}
                               };
            ActiveRecordMediator<Pais>.Create(pais);

            var paisId = pais.Id;

            pais = ActiveRecordMediator<Pais>.FindByPrimaryKey(paisId);
            var serializer = new NHibernateProxyJsonSerializer();
            serializer.Converters.Add(new NHibernateProxyJsonConverter());
            serializer.NullValueHandling = NullValueHandling.Ignore;
            serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

            var writer = new StringWriter();
            serializer.Serialize(new JsonTextWriter(writer), pais);
            var json = writer.ToString();

            Assert.IsNotNull(json);
            Assert.IsNotEmpty(json);

            var reader = new StringReader(json);
            var paistmp = (Pais) serializer.Deserialize(new JsonTextReader(reader), typeof (Pais));

            Assert.IsNotNull(paistmp);
            Assert.IsTrue(paistmp.Id == paisId);
            Assert.IsNotNull(paistmp.Estados);
            Assert.IsTrue(paistmp.Estados[0].Id == pais.Estados[0].Id);
            Assert.IsTrue(paistmp.Estados[0].Nombre == pais.Estados[0].Nombre);
            //Assert.IsNotNull(paistmp.Estados[0].Pais);
            //Assert.IsNotNull(paistmp.Estados[0].Pais.Id == paisId);
        }
    }
}